function giaihe2()
{
	var d=args[0]*args[4]-args[3]*args[1];
	var dx=args[2]*args[4]-args[5]*args[1];
	var dy=args[0]*args[5]-args[3]*args[2];
	
	if (d != 0)
	{
		var x = (dx - 0) / (d - 0);
		var y = (dy - 0) / (d - 0);
		return new Array(x , y);
	}
	else if (dx)
	{
		return 'Vo nghiem';
	}
	else
	{
		return 'Vo so nghiem';
	}	
}

function gauss_jordan(args, n)
{
	for (var i = 0; i < n; i++)
	{
		var h = i;
		for (var k = i + 1; k < n; k++)
		{
			if (Math.abs(args[k * (n + 1) + i]) > Math.abs(args[h * (n + 1) + i]))
			{
				h = k;
			}
		}
		if (args[h * (n + 1) + i] == 0)
		{
			if (args[h * (n + 1) + n] == 0)
			{	
				return 'HPT vo so nghiem';
			}
			else
			{	
				return 'HPT vo nghiem';
			}
		}
		if (h != i)
		{
			
			for (var j = i; j < n + 1; j++)
			{
				var temp = args[i * (n + 1) + j];
				args[i * (n + 1) + j] = args[h * (n + 1) + j];
				args[h * (n + 1) + j] = temp;
			}
		}
		var temp =  args[i * (n + 1) + i];
		
		for (var j = i; j < n + 1; j++)
		{
			args[i * (n + 1) + j] /= temp;
		}
		
		for (var k = 0; k < n; k++)
		{
			if (k == i)
				continue;
			var tiso = args[k * (n + 1) + i];
			for (var j = i + 1; j < n + 1; j++)
			{
				args[k * (n + 1) + j] -= tiso * args[i * (n + 1) + j];
			}
		}
	}
	var result = [];
	for (var i = 0; i < n; i++)
	{
		result[result.length] = args[i * (n + 1) + n];
	}
	return result;
}

function giaiPtBac2(args)
{
	// ax2 + bx + c = 0, a = args[0], b = args[1], c = args[2]
	var a = args[0] - 0;
	var b = args[1] - 0;
	var c = args[2] - 0;
	if (a != 0)
	{
		var delta = Math.pow(b, 2) - 4 * a * c;
		if (delta < 0)
		{
			return 'PT vo nghiem';
		}
		if (delta == 0)
		{
			return [-b / (2 * a)];
		}
		else
		{
			return [(-b + Math.sqrt(delta)) / (2 * a), (-b - Math.sqrt(delta)) / (2 * a)];
		}
	}
	else
	{
		if (b == 0)
		{
			if (c == 0)
			{
				return 'PT vo so nghiem';
			}
			else
			{
				return 'PT vo nghiem';
			}
		}
		else
		{
			return [-c / b];
		}
	}
}

function giaiPtBac3(args)
{
	var a = args[0] - 0;
	var b = args[1] - 0;
	var c = args[2] - 0;
	var d = args[3] - 0;
	
	if (a == 0)
	{	
		return giaiPtBac2([b, c, d]);
	}
	else
	{
		var delta = Math.pow(b, 2) - 3 * a * c;
		
		if (delta == 0)
		{
			var x = (-b + cuberoot((b * b * b - 27 * a * a * d))) / (3 * a);
			return [x];
		}
		else
		{
			var k = (9 * a * b * c - 2 * b * b * b - 27 * a * a * d) / (2 * Math.sqrt(Math.pow(Math.abs(delta), 3)));
			if (delta < 0)
			{
				var x = (Math.sqrt(-delta) * (cuberoot(k + Math.sqrt(k * k + 1)) + cuberoot(k - Math.sqrt(k * k + 1))) - b) / (3 * a);
				return [x];
			}
			else
			{
				if (Math.abs(k) <= 1)
				{
					var x1 = (2 * Math.sqrt(delta) * Math.cos(Math.acos(k) / 3)) / (3 * a);
					var x2 = (2 * Math.sqrt(delta) * Math.cos(Math.acos(k) / 3 - 2 * Math.PI / 3)) / (3 * a);
					var x3 = (2 * Math.sqrt(delta) * Math.cos(Math.acos(k) / 3 + 2 * Math.PI / 3)) / (3 * a);
					return [x1, x2, x3];
				}
				else
				{
					var x = (Math.sqrt(delta) * Math.abs(k) * (cuberoot(Math.abs(k) + Math.sqrt(k * k - 1)) + cuberoot(Math.abs(k) - Math.sqrt(k * k - 1))) - b * k) / (3 * a);
					return [x];
				}
			}
		}
		
	}
}

function cuberoot(x)
{
	if (x < 0)
	{
		return -(Math.pow(-x, 1 / 3))
	}
	else
	{
		return Math.pow(x, 1 / 3);
	}
}

function root(x, n)
{
	if (n == 0)
	{
		throw new Error('Math error');
	}
	if (x < 0)
	{
		if (n % 2 == 0)
		{
			throw new Error('Math error');
		}
		else
		{
			return -(Math.pow(-x, 1 / n))
		}
	}
	return Math.pow(x, 1 / n);
}

function power(x, n)
{
	// x^n;
	if (x < 0 && 0 < n < 1)
	{
		if (1 / n % 2 == 1)
		{
			return -(Math.pow(-x, 1 / n))
		}
		else 
		{
			throw new Error('Math error');
		}
	}
	return Math.pow(x, n);
}
